package com.dingding.order.core.confirm.dao.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dingding.common.core.base.dao.impl.BaseDaoImpl;
import com.dingding.common.core.beans.Userorder;
import com.dingding.common.core.beans.Userordert;
import com.dingding.common.core.util.ToolUtil;
import com.dingding.order.core.confirm.dao.IConfirmArriveDao;
import com.dingding.order.core.confirm.dao.IUserOrderDao;
import com.dingding.order.core.confirm.sql.ConfirmSQL;
import com.dingding.order.dto.confirm.PushAutoConfirmArrival;
import com.dingding.order.dto.confirm.PushConfirmArrival;

/**
 * @Description: 确认到达dao
 * @author fengshuonan
 * @date 2016年6月6日 上午11:27:37
 */
@Component("confirmArriveDao")
public class ConfirmArriveDaoImpl extends BaseDaoImpl<Userordert> implements IConfirmArriveDao {

	private Logger log = Logger.getLogger(this.getClass());

	@Autowired
	private IUserOrderDao userOrderTDao; // 正式订单的dao

	/**
	 * @Description: 获取乘客订单
	 * @param passengerId
	 *            乘客id
	 * @author fengshuonan
	 * @return Userordert 合乘订单bean
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public Userordert getPassengerOrder(String oid) {

		Userordert result = null;

		// 查找到用户的订单
		Criterion criterion = Restrictions.eq("oid", oid);// 订单状态是已支付

		try {
			// 获取订单信息
			result = super.queryCriteriaUnResult(criterion);

			// 修改订单状态
			if (result != null) {

				result.setOrderStatus(4);// 订单完成
				result.setEndTime(new Date());// 确认到达时间

			}

		} catch (Exception e) {
			log.error("查询用户合乘订单出错！ConfirmArriveDaoImpl:getPassengerOrder()", e);
		}

		return result;
	}

	/**
	 * @Description:获取所有合乘订单
	 * @param parOrderId  主订单Id
	 * @author fengshuonan
	 * @return List<Userordert> 所有合乘订单
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public List<Userordert> getAllCarShareOrder(String parOrderId) {

		List<Userordert> list = null;

		Criterion criterion = Restrictions.or(Restrictions.eq("oid", parOrderId),Restrictions.eq("parOrderId", parOrderId));

		try {
			list = this.queryCriteriaList(criterion);
		} catch (Exception e) {
			log.error("获取所有合乘订单出错！ConfirmArriveDaoImpl : getAllCarShareOrder()", e);
		}

		return list;
	}

	/**
	 * @Description:添加表到正式表
	 * @param userordert 临时表
	 * @author fengshuonan
	 * @return boolean true 添加成功  false 添加失败
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean addToUserorderTable(Userordert userordert) {

		Userorder userorder = new Userorder();

		BeanUtils.copyProperties(userordert, userorder);
		
		userorder.setPublishType(1); //1为私家车    2为出租车

		try {
			this.userOrderTDao.save(userorder);
		} catch (Exception e) {
			log.error("订单添加到正式表的时候出错！ConfirmArriveDaoImpl ： addToUserorderTable()  订单ID为："+userorder.getOid(), e);
			return false;
		}

		return true;
	}

	/**
	 * 
	 * @Title: deleteCurrentOrder 
	 * @Description: 移除订单
	 * @param userordert 订单信息
	 * @author YHQ 
	 * @return boolean true:成功  false:失败
	 * 
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED)
	public boolean deleteCurrentOrder(Userordert userordert) {

		try {
			super.delete(userordert);
		} catch (Exception e) {
			log.error("确认到达移除订单时出现异常   类：" + this.getClass() + " deleteCurrentOrder()", e);
			return false;
		}

		return true;
	}

	/**
	 * 
	 * @Title: getConfirmArrival 
	 * @param passengerId 乘客Id
	 * @param driverId 司机Id
	 * @author CHEHAUNBO
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public PushConfirmArrival getConfirmArrival(String passengerId, String driverId) {

		// 创建乘客支付成功，给司机推送Bean
		PushConfirmArrival pushConfirmArrival = new PushConfirmArrival();

		// 根据司机Id,获取司机信息
		Object[] obj = null;
		try {
			obj = super.sqlQueryUnResultObj(ConfirmSQL.PASSENGER_PAY_PUSH, passengerId, driverId);
		} catch (Exception e) {
			log.info("乘客支付获取推送内容时出现异常  类：" + this.getClass() + " getPassengerPayPushInfo()", e);
		}

		// 判断司机信息是否为空
		if (obj.length > 0) {

			// 将司机信息赋值给pushPassengerPay
			this.getPushConfirmArrival(obj, pushConfirmArrival);
		}

		return pushConfirmArrival;

	}

	public void getPushConfirmArrival(Object[] obj, PushConfirmArrival pushConfirmArrival) {

		// 司机Id(推送Id)
		if (obj[0] != null) {
			pushConfirmArrival.setDriverId(obj[0].toString());
		}
		// 设备类型
		if (obj[1] != null) {
			// 设备类型 1:iOS 2:Android
			String deviceType = obj[1].toString();

			if (deviceType.equals("1")) { // 设备类型为：iOS
				pushConfirmArrival.setLoginEquipment("iOS");
			} else if (deviceType.equals("2")) { // 设备类型为：Android
				pushConfirmArrival.setLoginEquipment("Android");
			}

		}
		// 司机电话
		if (obj[2] != null) {
			pushConfirmArrival.setTellPhone(obj[2].toString());
		}
		// 乘客姓名
		if (obj[3] != null) {
			pushConfirmArrival.setPassengerName(obj[3].toString());
		}
		// 车牌号
		if (obj[4] != null) {
			pushConfirmArrival.setLisencePlate(obj[4].toString());
		}
		// 司机名称
		if (obj[5] != null) {
			pushConfirmArrival.setDriverName(obj[5].toString());
		}

	}

	/**
	 * 
	 * @Title: getUserOrdertByOverTime
	 * @Description: 获取自动确认到达的订单列表
	 * @author yuHaiQing
	 * @return List<Userordert>    返回类型
	 * 
	 */
	@Override
	@Transactional(readOnly=true)
	public List<Userordert> getUserOrdertByOverTime() {
		
		try {
			
			List<Object[]> objects = super.sqlQueryListObj(ConfirmSQL.AUTO_CONFIRM_ARRIVAL_ORDER_LIST, false);
			
			if(objects != null && objects.size() > 0){
				
				return getUserOrders(objects);
			}
			
		} catch (Exception e) {
			log.error("获取自动确认到达的订单列表时出现异常   类："+this.getClass()+" getUserOrdertByOverTime()", e);
		}
		
		return null;
	}

	/**
	 * 
	 * @Title: getUserOrders 
	 * @Description: 自动确认到达订单信息 
	 * @param objects 订单信息
	 * @author YHQ 
	 * @return void    返回类型
	 * 
	 */
	public List<Userordert> getUserOrders(List<Object[]> objects){
		
		List<Userordert> userorderts = new ArrayList<Userordert>();
		
		for (Object[] obj : objects) {
			
			Userordert userordert = new Userordert();
			
			if(obj[0] != null){
				userordert.setOid(obj[0].toString());
			}
			if(obj[1] != null){
				userordert.setPlaceOrderUserId(obj[1].toString());
			}
			if(obj[2] != null){
				userordert.setPayCity(obj[2].toString());
			}
			if(obj[3] != null){
				userordert.setSetOutTime((Date)obj[3]);
			}
			if(obj[4] != null){
				userordert.setStartAddress(obj[4].toString());
			}
			if(obj[5] != null){
				userordert.setStartLongitude(ToolUtil.castDouble(obj[5]));
			}
			if(obj[6] != null){
				userordert.setStartLatitude(ToolUtil.castDouble(obj[6]));
			}
			if(obj[7] != null){
				userordert.setEndAddress(obj[7].toString());
			}
			if(obj[8] != null){
				userordert.setEndLongitude(ToolUtil.castDouble(obj[8]));
			}
			if(obj[9] != null){
				userordert.setEndLatitude(ToolUtil.castDouble(obj[9]));
			}
			if(obj[10] != null){
				userordert.setDistance(new BigDecimal(obj[10].toString()));
			}
			if(obj[11] != null){
				userordert.setPayMoney(new BigDecimal(obj[11].toString()));
			}
			if(obj[12] != null){
				userordert.setAlipayMoney(new BigDecimal(obj[12].toString()));
			}
			if(obj[13] != null){
				userordert.setPayType(ToolUtil.castInteger(obj[13]));
			}
			if(obj[14] != null){
				userordert.setUcId(obj[14].toString());
			}
			if(obj[15] != null){
				userordert.setCouponMoney(new BigDecimal(obj[15].toString()));
			}
			if(obj[16] != null){
				userordert.setTotalMoney(new BigDecimal(obj[16].toString()));
			}
			if(obj[17] != null){
				userordert.setNumber(ToolUtil.castInteger(obj[17]));
			}
			if(obj[18] != null){
				userordert.setRedBag(ToolUtil.castInteger(obj[18]));
			}
			if(obj[19] != null){
				userordert.setMessage(obj[19].toString());
			}
			if(obj[20] != null){
				userordert.setPayDistance(new BigDecimal(obj[20].toString()));
			}
			if(obj[21] != null){
				userordert.setMarker(obj[21].toString());
			}
			if(obj[22] != null){
				userordert.setGrabOrderUserId(obj[22].toString());
			}
			if(obj[23] != null){
				userordert.setGrabOrderTime((Date)obj[23]);
			}
			if(obj[24] != null){
				userordert.setOrderStatus(ToolUtil.castInteger(obj[24]));
			}
			if(obj[25] != null){
				userordert.setCreateTime((Date)obj[25]);
			}
			if(obj[26] != null){
				userordert.setPayTime((Date)obj[26]);
			}
			if(obj[27] != null){
				userordert.setEndTime((Date)obj[27]);
			}
			if(obj[28] != null){
				userordert.setCancelTime((Date)obj[28]);
			}
			if(obj[29] != null){
				userordert.setOrderType(ToolUtil.castInteger(obj[29]));
			}
			if(obj[30] != null){
				userordert.setCancelType(ToolUtil.castInteger(obj[30]));
			}
			if(obj[31] != null){
				 userordert.setCancelNumber(ToolUtil.castInteger(obj[31]));
			}      
			if(obj[32] != null){
				 userordert.setCancelDesc(obj[32].toString());
			}      
			if(obj[33] != null){
				 userordert.setIsLongDistance(ToolUtil.castInteger(obj[33]));
			}      
			if(obj[34] != null){
				 userordert.setPsgRcvIntegral(new BigDecimal(obj[34].toString()));
			}      
			if(obj[35] != null){
				 userordert.setDriverRcvIntegral(new BigDecimal(obj[35].toString()));
			}      
			if(obj[36] != null){
				 userordert.setCancelUserId(obj[36].toString());
			}      
			if(obj[37] != null){
				 userordert.setIsComment(ToolUtil.castInteger(obj[37]));
			}      
			if(obj[38] != null){
				 userordert.setIsCarShare(ToolUtil.castInteger(obj[38]));
			}      
			if(obj[39] != null){
				 userordert.setSetOutCondition(ToolUtil.castInteger(obj[39]));
			}
			if(obj[40] != null){
				 userordert.setFreeSeat(ToolUtil.castInteger(obj[40]));
			}
			if(obj[41] != null){
				 userordert.setParOrderId(obj[41].toString());
			}
			if(obj[42] != null){
				userordert.setIsChildren(ToolUtil.castInteger(obj[42]));
			}
			if(obj[43] != null){
				userordert.setLastDate((Date)obj[43]);
			}
			
			userorderts.add(userordert);
		}
		
		return userorderts;
	}

	
	/**
	 * 
	 * @Title: getConfirmOrders
	 * @Description: 获取自动确认到达推送Bean
	 * @param oid 订单Id
	 * @param passengerId 乘客Id
	 * @param driverId 司机Id
	 * @author yuHaiQing
	 * @return PushAutomaticCancelOrder    返回类型
	 * 
	 */
	@Override
	@Transactional(readOnly=true)
	public PushAutoConfirmArrival getConfirmOrders(String passengerId, String driverId) {
		
		PushAutoConfirmArrival pushAutomaticCancelOrder = new PushAutoConfirmArrival();
		
		// 根据司机Id,获取司机信息
		Object[] obj = null;
		try {
			obj = super.sqlQueryUnResultObj(ConfirmSQL.AUTO_CONFIRM_ARRIVAL_LIST, passengerId, driverId);
		} catch (Exception e) {
			log.info("乘客支付获取推送内容时出现异常  类：" + this.getClass() + " getPassengerPayPushInfo()", e);
		}

		// 判断司机信息是否为空
		if (obj != null) {

			// 将司机信息赋值给pushPassengerPay
			this.getPushAutoConfirmArrival(pushAutomaticCancelOrder,obj);
		}
		
		
		return pushAutomaticCancelOrder;
	}

	/**
	 * 
	 * @Title: getPushAutoConfirmArrival
	 * @Description: 获取自动确认到达所需Bean
	 * @param pushAutoConfirmArrival 自动确认到达通知Bean
	 * @param obj 自动确认到达通知内容
	 * @author yuHaiQing
	 * @return PushAutoConfirmArrival    返回类型
	 * 
	 */
	public PushAutoConfirmArrival getPushAutoConfirmArrival(PushAutoConfirmArrival pushAutoConfirmArrival,Object[] obj){
		
		// 订单Id
		if (obj[0] != null) {
			pushAutoConfirmArrival.setOid(obj[0].toString());
		}
		// 订单状态
		if (obj[1] != null) {
			pushAutoConfirmArrival.setOrderStatus(ToolUtil.castInteger(obj[1]));
		}
		// 下单人角色
		if (obj[2] != null) {
			pushAutoConfirmArrival.setOrderType(ToolUtil.castInteger(obj[2]));
		}
		// 乘客Id
		if (obj[3] != null) {
			pushAutoConfirmArrival.setPassengerId(obj[3].toString());
		}
		// 乘客设备类型
		if (obj[4] != null) {
			Integer deviceType = ToolUtil.castInteger(obj[4]);
			pushAutoConfirmArrival.setPassengerEquipment(deviceType == 1 ? "iOS" : "Android");
		}
		// 乘客姓名
		if (obj[5] != null) {
			pushAutoConfirmArrival.setPassengerName(obj[5].toString());
		}
		// 乘客电话
		if (obj[6] != null) {
			pushAutoConfirmArrival.setPassengerPhone(obj[6].toString());
		}
		// 司机id
		if (obj[7] != null) {
			pushAutoConfirmArrival.setDriverId(obj[7].toString());
		}
		// 司机登录设备
		if (obj[8] != null) {
			Integer deviceType = ToolUtil.castInteger(obj[8]);
			pushAutoConfirmArrival.setDriverEquipment(deviceType == 1 ? "iOS" : "Android");
		}
		// 司机昵称
		if (obj[9] != null) {
			pushAutoConfirmArrival.setDriverName(obj[9].toString());
		}
		// 司机电话
		if (obj[10] != null) {
			pushAutoConfirmArrival.setDriverPhone(obj[10].toString());
		}
		// 支付总金额
		if (obj[11] != null) {
			pushAutoConfirmArrival.setTotalMoney(new BigDecimal(obj[11].toString()));
		}
		// 乘客推送id
		if (obj[12] != null) {
			pushAutoConfirmArrival.setPassengerAliasId(obj[12].toString());
		}
		// 司机推送id
		if (obj[13] != null) {
			pushAutoConfirmArrival.setDriverAliasId(obj[13].toString());
		}
		// 总里程
		if (obj[14] != null) {
			pushAutoConfirmArrival.setDistance(new BigDecimal(obj[14].toString()));
		}
		// 支付宝支付金额
		if (obj[15] != null) {
			pushAutoConfirmArrival.setAlipayMoney(new BigDecimal(obj[15].toString()));
		}
		// 叮币支付金额
		if (obj[16] != null) {
			pushAutoConfirmArrival.setPayMoney(new BigDecimal(obj[16].toString()));
		}
		// 优惠金额
		if (obj[17] != null) {
			pushAutoConfirmArrival.setCouponMoney(new BigDecimal(obj[17].toString()));
		}

		if (obj.length > 18) {
			// 下单时间
			if (obj[18] != null) {
				pushAutoConfirmArrival.setCreateTime((Date) obj[18]);
			}
			// 出发时间
			if (obj[19] != null) {
				pushAutoConfirmArrival.setSetOutTime((Date) obj[19]);
			}
			// 起点
			if (obj[20] != null) {
				pushAutoConfirmArrival.setStartAddress(obj[20].toString());
			}
			// 终点
			if (obj[21] != null) {
				pushAutoConfirmArrival.setEndAddress(obj[21].toString());
			}
			// 1:普通 2:长途
			if (obj[22] != null) {
				pushAutoConfirmArrival.setIsLongDistance(ToolUtil.castInteger(obj[22]));
			}
			// 用户优惠券Id
			if (obj[23] != null) {
				pushAutoConfirmArrival.setUcId(obj[23].toString());
			}
			// 车牌号
			if (obj[24] != null) {
				pushAutoConfirmArrival.setLisencePlate(obj[24].toString());
			}
		}
		
		return pushAutoConfirmArrival;
	}

	/**
	 * 
	 * @Title: updateOrderInfo 
	 * @Description: 修改订单信息 
	 * @param userordert 待修改的订单信息
	 * @author YHQ 
	 * @return boolean    返回类型
	 * 
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public boolean updateOrderInfo(Userordert userordert) {
		
		try {
			super.update(userordert);
			
			return true;
		} catch (Exception e) {
			log.error("修改订单信息时出现异常   类："+this.getClass()+" updateOrderInfo()", e);
		}
		
		return false;
	}
	
}
